//
//  StatusCell.swift
//  sinaDemo
//
//  Created by mac on 15/12/18.
//  Copyright © 2015年 zhouxubin. All rights reserved.
//

import UIKit
import SnapKit

class StatusCell: UITableViewCell {
    
    // 记录底部视图的顶部约束
    var bottomViewTopContraints: Constraint?
    
    // 模型数组
    var status: Status? {
        didSet {
            statusCellTopView.status = status
            
            // 卸载掉约束
            bottomViewTopContraints?.uninstall()
            if let retweetStatus = status?.retweeted_status {
//                retweetView.retweetStatus = retweetStatus
                // 如果有转发微博的数据,就显示转发微博
                statusCellBottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomViewTopContraints = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                
                // 设置数据源
                retweetView.retweetStatus = retweetStatus
                retweetView.hidden = false
                
            }else {
                // 原创微博
                // 把约束去掉
                statusCellBottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomViewTopContraints = make.top.equalTo(statusCellTopView.snp_bottom).constraint
                })
                retweetView.hidden = true
            }
        }
    }
    
    // MARK: 懒加载子控件
    // 顶部视图
    private var statusCellTopView: StatusCellTopView = StatusCellTopView()
    
    // 底部视图
    private var statusCellBottomView: StatusCellBottomView = StatusCellBottomView()
    // 转发微博视图
    private lazy var retweetView: StatusCellRetweetView = StatusCellRetweetView()
    
    
    // 初始化方法
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        // 背景颜色
        
        
        // 添加子控件
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: 设置子控件
    private func setUpUI() {
        contentView.addSubview(statusCellTopView)
        contentView.addSubview(statusCellBottomView)
        contentView.addSubview(retweetView)
        
        // 设置约束
        // 头部视图
        statusCellTopView.snp_makeConstraints { (make) -> Void in
            make.left.top.right.equalTo(contentView)
        }
        
        // 转发微博视图
        retweetView.snp_makeConstraints { (make) -> Void in
            make.left.right.equalTo(contentView)
            make.top.equalTo(statusCellTopView.snp_bottom)
//            make.height.equalTo(100)
        }
        
        // 底部视图
        statusCellBottomView.snp_makeConstraints { (make) -> Void in
            make.left.right.equalTo(self)
            self.bottomViewTopContraints = make.top.equalTo(retweetView.snp_bottom).constraint
            make.height.equalTo(35)
        }
        
        // 设置contentview的约束
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.bottom.equalTo(statusCellBottomView.snp_bottom)
        }
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
